/*
页面定宽设计插件By刺鸟
无对外方法，自动处理
*/
~function () {
	var MYWIDTH = CONF.width;
	//PC模式下，每次清空缓存，便于调试
	if(window.navigator.platform=="Win32"){
		localStorage.removeItem('ciniao_zoom_'+MYWIDTH);
	}
	
	Q.fn({
		"getZoom" : function(){
			//获取缩放比例
			if(localStorage['ciniao_zoom_'+MYWIDTH]){
				return localStorage['ciniao_zoom_'+MYWIDTH];
			}
			var width = document.body.clientWidth,
			zoom = width/MYWIDTH;
			localStorage['ciniao_zoom_'+MYWIDTH] = zoom;
			return zoom;
		},
		"resetZoom" : function(jqselect){
			$(jqselect).css('zoom',1/Q.getZoom());
		},
		"fixWidth" : function(){
			//设置页面宽度
			if(window.NORESIZE==true)return;
			document.body.style.zoom = Q.getZoom();
		},
		"_popWindowArray":[],
		//处理popWindow时，坐标&宽度问题
		"uexWindow" : {
			openPopover : function(inPopName,inDataType,inUrl,inData,inX,inY,inWidth,inHeight,inFontSize,inFlag){
				Q._popWindowArray.push(inPopName);
				var data = {
					left:Q.posZoom(inX),
					top:Q.posZoom(inY),
					width:Q.sizeZoom(inWidth),
					height:Q.sizeZoom(inHeight)
				};
				$('#'+inPopName).data('boundZoom',JSON.stringify(data));
				var flag = inFlag;
				if(inUrl.substr(0,7)=='http://'){
					flag = '512';
				}
				uexWindow.openPopover(inPopName,inDataType,inUrl,inData,data.left,data.top,data.width,data.height,inFontSize,flag);
			}
			,ezAutoResize : function(){
				if(!$('.ezAutoResize').attr('id'))return;
				var inPopName = ($('.ezAutoResize').attr('id')).replace('_FRAME','');
				localStorage.removeItem('ciniao_zoom_'+MYWIDTH);
				document.body.style.zoom = 1;
				Q.fixWidth();
				if(!Q.$$(inPopName))return;
				
				var bound = Q.$$(inPopName).parentNode.getBoundingClientRect();
				if(!Q.isAppCan){
					$('#'+inPopName).css({"width":bound.width,"height":bound.height});
				}else{
					var data = {
						left:Q.posZoom(bound.left),
						top:Q.posZoom(bound.top),
						width:Q.sizeZoom(bound.width),
						height:Q.sizeZoom(bound.height)
					};
					$('#'+inPopName).data('boundZoom',JSON.stringify(data));
					uexWindow.setPopoverFrame(inPopName,data.left,data.top,data.width,data.height);
					
					for(var i=0;i<Q._popWindowArray.length;i++){
						(function(i){
							setTimeout(function(){
								uexWindow.evaluatePopoverScript('',Q._popWindowArray[i],"Q.fixWidth();")	
							},500);
						})(i);
					}
				}
			}
		},
		"ifPodNeedZoom" : function(){
			//部分设备上getComputedStyle取到的尺寸已经是缩放后的尺寸
			//无需posZoom，这里对这样的设备进行判断
			var testDiv = document.createElement("div");
			testDiv.id = "testDiv";
			testDiv.style.width = "width:100%"
			testDiv.style.height = "10px";
			document.body.appendChild(testDiv);
			document.body.style.zoom = 1;
			
			var s=window.getComputedStyle(Q.$$('testDiv'),null),
			_width1 = s.width;
			document.body.style.zoom = 0.5;
			s=window.getComputedStyle(Q.$$('testDiv'),null),
			_width2 = s.width;
			
			document.body.removeChild(testDiv);
			document.body.style.zoom = 1;
			
			Q.posZoom  = function(v){
				return Math.round(v*Q.getZoom());
			};
			
			if(_width1 == _width2){
				Q.sizeZoom = function(v){
					return v;
				};
			}else{
				Q.sizeZoom  = function(v){
					return Math.round(v*Q.getZoom());
				};
			}
		}
	});
	
	Q.ifPodNeedZoom();
	if(localStorage['ciniao_zoom_'+MYWIDTH]){
		//如果已计算出缩放比例，则直接缩放，避免页面先显示完毕后才缩放导致闪烁一下
		Q.fixWidth();
	}else{
		Q.$(function(){
			Q.fixWidth();
		});	
	}
	
	Q.resize(function(){
		Q.uexWindow.ezAutoResize();	
	});
}(); 